<?php

class api_51{
	
		//固定配置 
	public $api_online_url	=	'http://ws.tongyedns.com:8000/ltips/services/';	//	接口地址
	public $api_test_url	=	'http://wstest.51book.com:55779/ltips/services/';//测试地址
	public $api_test_z_url	=	'http://219.141.233.66:55798/ltips/services/';//测试地址
	public $api_url			=	'';				//	接口地址
	public $api_key			=	'JSKM';			//	key值 SDFH JSKM
	public $sign			=	'3EB*j4ki';		//	签名 vvFsiRcB 3EB*j4ki
	public $agencyCode		=	'JSKM';			//	公司账号 SDFH
	public $logs			=	array();		//	日志
	public $_result			=	array('status'=>'success','msg'=>'','data'=>'');
	public $api_method		=	array(
									'refundapply'=>'applyPolicyOrderRefundServiceRestful1.0/applyPolicyOrderRefund',//退票
									'ordercreateex'=>'createOrderByPassengerServiceRestful1.0/createOrderByPassenger',// 创建订单
									'ordercancel'=>'cancelOrderServiceRestful1.0/cancelOrder',//取消订单
									'flightquery'=>'getAvailableFlightWithPriceAndCommisionServiceRestful1.0/getAvailableFlightWithPriceAndCommision',//查询
									'refundquery'=>'getRefundDetailServiceRestful1.0/getRefundDetail',//退票查询
									'orderquery'=>'getOrderStatusServiceRestful1.0/getOrderStatus',//订单查询
									'orderpay'=>'payServiceRestful1.0/pay',//支付									
									'getPolicy'=>'getPolicyByIdServiceRestful1.0/getPolicyById',//获取政策
									'getFullPolicy'=>'getPolicyServiceRestful1.0/getPolicy',//全取政策
									'changeapply'=>'',//改签
									'changequery'=>'',//改签查询
									'changepay'=>''//改签补款
								);
	public function __construct($config=''){
		$this->api_url	=	$this->api_online_url;
		//$this->api_test();
	}
	public function api_test(){
	    $this->api_key='SDFH';
	    $this->sign='vvFsiRcB';
	    $this->agencyCode='SDFH';
		$this->api_url	=	$this->api_test_url;
	
	}
	//查询航线
	public function flightquery($param){		
		//默认参数
		$param['onlyAvailableSeat']			= 0;	//只返回可用舱位 0 只返回可用舱位;1 返回完整舱位列表;即只返回舱位数量为1～9或为A的舱位;
		$param['onlyNormalCommision']		= 1;	//是否包括特殊政策 0 仅返回普通政策; 1  允许返回特殊政策;
		$param['onlyOnWorkingCommision']	= 1;	//是否只返回在工作时间内政策  0不做限制 1仅返回当前仍在工作时间的政策
		$param['onlySelfPNR']				= 0;	//是否可更换PNR出票 0可以更换PNR后出票 1只用自己的PNR出票
		$param['param2']					= 0;	//备用参数	当没有政策的情况下是否显示舱位，1显示政策数据为-1 0不显示
		$param['agencyCode']				=	$this->api_key;	//公司代码
		//签名信息MD5加密(agencyCode+dstCity+onlyAvailableSeat+onlyNormalCommision+onlyOnWorkingCommision+onlySelfPNR+orgCity+安全码)加密为32位小写。
		$sign								=	$param['agencyCode'].$param['dstAirportCode'].$param['onlyAvailableSeat'].$param['onlyNormalCommision'].$param['onlyOnWorkingCommision'].$param['onlySelfPNR'].$param['orgAirportCode'].$this->sign;
		$this->logs['sign']					=	$sign;
		$param['sign']						=	strtolower(MD5($sign));
		$res	=	$this->execute('flightquery',$param);
		if($this->_result['status'] == 'success'){
			$res	=	$this->_result['data'];
		}else{
			return $this->_result;
		}
		foreach($res['flightItems']['flights'] as $k=>$v){
			if(isset($v['seatItems'][0])){
				$seatItems	=	$v['seatItems'];
			}else{
				$seatItems[0]	=	$v['seatItems'];
			}
			//仓位重组
			foreach($seatItems as $sv){
				$aar[$k]['flightNo']	=	$v['flightNo'];
				$aar[$k]['depCode']		=	$v['orgCity'];
				$aar[$k]['arrCode']		=	$v['dstCity'];
				$seatcode	=	strval($sv['seatCode']);
				$aar[$k]['seatClass']	=	$seatcode;
				$aar[$k]['policyId']	=	$sv['policyData']['policyId'];						
				$aar[$k]['depDate']		=	$res['flightItems']['date'];
				$aar[$k]['depTime']		=	$v['depTime'];
				$aar[$k]['arrTime']		=	$v['arriTime'];
				$aar[$k]['planeModel']	=	$v['planeType'];
				$aar[$k]['parPrice']	=	$sv['parPrice'];
				$aar[$k]['price']		=	$sv['settlePrice'];;
				$aar[$k]['fuelTax']		=	$res['flightItems']['audletFuelTax'];
				$aar[$k]['airportTax']	=	$res['flightItems']['audletAirportTax'];
			}
				
		}
		$this->_result['data']	=	$aar;
		return $this->_result['data'];
		//数据重组		
		$flightinfo	=	array();
		$cabininfo	=	array();
		$prices		=	array();   

		foreach($res['flightItems']['flights'] as $v){
			//航班信息重组
			$deptime	=	$res['flightItems']['date'].' '.
							substr($v['depTime'],0,2).':'.substr($v['depTime'],2,2).':00';
			$arrtime	=	$res['flightItems']['date'].' '.
							substr($v['arriTime'],0,2).':'.substr($v['arriTime'],2,2).':00';
			$flightinfo[$v['flightNo']]['dep']		= $v['orgCity'];
			$flightinfo[$v['flightNo']]['depname']	= $this->airportname($v['orgCity']);
			$flightinfo[$v['flightNo']]['depterm']	= $v['orgJetquay'];
			$flightinfo[$v['flightNo']]['deptime']	= $deptime;
			$flightinfo[$v['flightNo']]['arr']		= $v['dstCity'];
			$flightinfo[$v['flightNo']]['arrname']	= $this->airportname($v['dstCity']);
			$flightinfo[$v['flightNo']]['arrterm']	= $v['dstJetquay'];
			$flightinfo[$v['flightNo']]['arrtime']	= $arrtime;
			$flightinfo[$v['flightNo']]['aircode']	= substr($v['flightNo'],0,2);
			$flightinfo[$v['flightNo']]['airtype']	= $v['planeType'];
			$flightinfo[$v['flightNo']]['fueltax']	= $res['flightItems']['audletFuelTax'];
			$flightinfo[$v['flightNo']]['airporttax']	= $res['flightItems']['audletAirportTax'];
			$flightinfo[$v['flightNo']]['share']		= $v['codeShare'];
			$flightinfo[$v['flightNo']]['stop']			= $v['stopnum'];
			if(isset($v['seatItems'][0])){
				$seatItems	=	$v['seatItems'];
			}else{
				$seatItems[0]	=	$v['seatItems'];
			}
			//仓位重组
			foreach($seatItems as $sv){
					$seatcode	=	strval($sv['seatCode']);
					$cabininfo[$v['flightNo']][$seatcode]['cabinname']	= $sv['seatMsg'];
					$cabininfo[$v['flightNo']][$seatcode]['canauto']		= true;
					$cabininfo[$v['flightNo']][$seatcode]['ticketnum']	= $sv['seatStatus'];
					$cabininfo[$v['flightNo']][$seatcode]['agio']			= $sv['discount'];
					$cabininfo[$v['flightNo']][$seatcode]['policyId']		= $sv['policyData']['policyId'];
					$cabininfo[$v['flightNo']][$seatcode]['inter']		= '51book';
					//价格重组
					$prices[$v['flightNo']][$seatcode]['ADT']['cabinprice']	= $sv['parPrice'];
					$prices[$v['flightNo']][$seatcode]['ADT']['price']	= $sv['settlePrice'];	

			}				
		}
		$data['flightinfo']	=	$flightinfo;
		$data['cabininfo']	=	$cabininfo;
		$data['prices']		=	$prices;

		$this->_result['data']		=	$data;
		return $this->_result;
	}
	//创建订单
	public function ordercreateex($param){
		//$param['notifiedUrl']		=	'http://www.zhuna.cn/jipiao/call51/orderpay';//被通知的url地址
		//$param['paymentReturnUrl']	=	'http://www.zhuna.cn/jipiao/call51/notify';//支付完成后返回的url地址
		$param['allowSwitchPolicy']	=	0;//允许帮选政策
		$param['needSpeRulePolicy']	=	0;//是否包括特殊政策
		$param['param1']			=	'';

		$param['agencyCode']	=	$this->api_key;	//公司代码
		$sign					=	$param['agencyCode'].$param['policyId'].$this->sign;
		$this->logs['sign']		=	$sign;
		$param['sign']			=	strtolower(MD5($sign));
		//$param	=	new SoapClient('http://cxf.apache.org/bindings/xformat',$param);
		//$param['pnrInfo']		=	$param['pnrInfo'];
		/*unset($param['pnrInfo']);*/
		
		$this->execute('ordercreateex',$param);
			//echo "<pre>";
			//print_r($param);
		return $this->_result;
	}
	//订单付款
	public function orderpay($param){
		$param['agencyCode']	=	$this->api_key;	//公司代码
		$sign					=	$param['agencyCode'].$param['orderNo'].$param['payType'].$param['payerLoginName'].$this->sign;
		$this->logs['sign']		=	$sign;
		$param['sign']			=	strtolower(MD5($sign));
		$this->execute('orderpay',$param);
		return $this->_result;
	}
	//订单查询
	public function orderquery($param){
		$param['agencyCode']	=	$this->api_key;	//公司代码
		$sign					=	$param['agencyCode'].$param['orderNo'].$this->sign;
		$this->logs['sign']		=	$sign;
		$param['sign']			=	strtolower(MD5($sign));
		$this->execute('orderquery',$param);
		return $this->_result;
	}
	//取消订单
	public function ordercancel($param){
		$param['agencyCode']	=	$this->api_key;	//公司代码
		$sign					=	$param['agencyCode'].$param['canclePNR'].$param['orderNo'].$this->sign;
		$this->logs['sign']		=	$sign;
		$param['sign']			=	strtolower(MD5($sign));
		$this->execute('ordercancel',$param);
		return $this->_result;
	}
	//退票
	public function refundapply($param){
		$param['agencyCode']	=	$this->api_key;	//公司代码
		$sign					=	$param['actionType'].$param['agencyCode'].$this->sign;
		$this->logs['sign']		=	$sign;
		$param['sign']			=	strtolower(MD5($sign));
		$this->execute('refundapply',$param);
		return $this->_result;
	}
	//退票查询
	public function refundquery($param){
		$param['agencyCode']	=	$this->api_key;	//公司代码
		$sign					=	$param['agencyCode'].$param['refundNo'].$this->sign;
		$this->logs['sign']		=	$sign;
		$param['sign']			=	strtolower(MD5($sign));
		$this->execute('refundquery',$param);
		return $this->_result;
	}
	//获取政策
	public function getPolicy($param){
		$param['agencyCode']	=	$this->api_key;	//公司代码
		$sign					=	$param['agencyCode'].$param['policyId'].$this->sign;
		$this->logs['sign']		=	$sign;
		$param['sign']			=	strtolower(MD5($sign));
		$this->execute('getPolicy',$param);
		return $this->_result;
	}
	//全取政策
	public function getFullPolicy($param){
		$param['needSpePricePolicy']	=	1;
		$param['needSpeRulePolicy']		=	1;
		$param['agencyCode']	=	$this->api_key;	//公司代码
		$sign					=	$param['agencyCode'].$param['needSpePricePolicy'].$param['needSpeRulePolicy'].$param['pageNo'].$param['rowPerPage'].$this->sign;
		$this->logs['sign']		=	$sign;
		$param['sign']			=	strtolower(MD5($sign));
		$this->execute('getFullPolicy',$param);
		return $this->_result;
	}
	//{
		//改签
		public function changeapply(){
			$param_field	=	"";
		}
		//改签查询
		public function changequery(){
			$param_field	=	"";
			$res	=	$this->execute('refundapply',$param);
			return $res['code'];
		}
		//改签补款
		public function changepay(){

			$res	=	$this->execute('refundapply',$param);
			return $res['code'];
		}

	//}	
	//数组转换xml
	public function arrtoxml($arr,$item){
		$tmpstr	=	'';
		$tag	=	'1';
		foreach($arr as $k=>$v){
			if(is_numeric($k)){
				$k	=	$item;
				$tag	=	'0';
			}		
			if(is_array($v)){
				$tmpstr	.=	$this->arrtoxml($v,$k);	
			}else{		
				$tmpstr	.=	'<'.$k.'>'.$v.'</'.$k.'>';
			}
		}
		if($tag==1){
			if(!empty($item)){
				$tmpstr	=	'<'.$item.'>'.$tmpstr.'</'.$item.'>';
			}
		}
		return $tmpstr;
	}
	public function execute($server,$param){
		$url	=	$this->api_url.$this->api_method[$server];
		$this->logs['url']		=	$url;
		$this->logs['param']	=	$param;
		$result	=	$this->PostRequest($url,$param);
		$this->logs['result']	=	$result;
		//$res	=	simplexml_load_string($result);
		//$res	=	xml2arr ($res); 
		$xml=new SimpleXMLElement($result);
		$res=json_decode(json_encode($xml),true);

		if(!isset($res['returnCode']) || $res['returnCode'] !== 'S'){
			$this->_result['status']	=	'error';
			$this->_result['msg']		=	isset($res['returnMessage']) ? $res['returnMessage'] : '接口出错，详见api_51->logs["result"]' ;
			return false;
		}else{
			$this->_result['data']	=	$res;
			return true;
		}		
	}
	 //发起请求
	public function PostRequest($url,$post){			
		//$header[] = "Content-type: text/xml";//定义content-type为xml
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 300);	//设置超时时间120秒
		//curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
		$response = curl_exec($ch);
		$this->requeryno	=	curl_errno($ch);
		curl_close($ch);
		return $response;
	}
	   	//soap调用
	public  function soapRequest($url,$data){
		$data = array('request' =>$data);
		$options = array(
			'trace' 				=> true,
			'encoding' 				=> 'UTF-8',
			'connection_timeout' 	=> 120,
			'soap_version' 			=> SOAP_1_1,
			'compression'			=>SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP,
			'keep_alive'			=>true
		);
		try {
			$soapclient = new Soapclient($url, $options);
		}catch (Exception $e ){
			$this->logs['soap']['client'][]	=	$e;
			return false;
		}
		$funarr	=	$soapclient->__getFunctions();
		$fun	=	substr($funarr[0],strpos($funarr[0],' ')+1,strpos($funarr[0],'(')-strpos($funarr[0],' ')-1);
		try {
			//$back=$soapclient->__call($fun,$data);
			$back=$soapclient->$fun($data);

		}catch (Exception $e ){
		  
			$this->logs['soap'][$fun][]	=	$e;

			$s=$soapclient->__getLastRequest();
			print_r($s);exit;
			return false;
		}
					//$s=$soapclient->__getLastRequest();
			//print_r($s);
		return $back;
	}
	public function airportname($code){
		$codearr	=	array('AKU'=>'阿克苏' ,'AAT'=>'阿勒泰' ,'AKA'=>'安康' ,'AQG'=>'安庆' ,'AOG'=>'鞍山' ,
		'AEB'=>'百色' ,'BSD'=>'保山' ,'BAV'=>'包头' ,'BHY'=>'北海' ,'BJS'=>'北京' ,'PEK'=>'北京首都' ,'NAY'=>'北京南苑' ,'BPL'=>'博乐' ,
		'CGQ'=>'长春' ,'CGD'=>'常德' ,'BPX'=>'昌都' ,'CSX'=>'长沙' ,'CIH'=>'长治' ,'CZX'=>'常州' ,'CHG'=>'朝阳' ,'CTU'=>'成都' ,'CIF'=>'赤峰' ,'CKG'=>'重庆' ,'NBS'=>'长白山' ,
		'DLC'=>'大连' ,'DLU'=>'大理' ,'DDG'=>'丹东' ,'DAT'=>'大同' ,'DAX'=>'达县' ,'DIG'=>'迪庆' ,'DOY'=>'东营' ,'DNH'=>'敦煌' ,'DQA'=>'大庆' ,'LUM'=>'德宏' ,
		'DSN'=>'鄂尔多斯' ,'ENH'=>'恩施' ,'ERL'=>'二连浩特' ,
		'FUO'=>'佛山' ,'FOC'=>'福州' ,'FUG'=>'阜阳' ,
		'KOW'=>'赣州' ,'GOQ'=>'格尔木' ,'GYS'=>'广元' ,'CAN'=>'广州' ,'KWL'=>'桂林' ,'KWE'=>'贵阳' ,'GYU'=>'固原' ,
		'HAK'=>'海口' ,'HLD'=>'海拉尔' ,'HMI'=>'哈密' ,'HDG'=>'邯郸' ,'HGH'=>'杭州' ,'HZG'=>'汉中' ,'HRB'=>'哈尔滨' ,'HFE'=>'合肥' ,'HEK'=>'黑河' ,'HTN'=>'和田' ,'HET'=>'呼和浩特' ,'TXN'=>'黄山' ,'HYN'=>'黄岩' ,'HJJ'=>'怀化' ,'HIA'=>'淮安' ,'JZH'=>'黄龙' ,
		'JMU'=>'佳木斯' ,'JGN'=>'嘉峪关' ,'TNA'=>'济南' ,'JDZ'=>'景德镇' ,'JGS'=>'井冈山' ,'JNG'=>'济宁' ,'JNZ'=>'锦州' ,'JIU'=>'九江' ,'JZH'=>'九寨沟' ,'JXA'=>'鸡西' ,'JJN'=>'晋江' ,'JHG'=>'景洪' ,
		'KJI'=>'喀纳斯' ,'KRY'=>'克拉玛依' ,'KHG'=>'喀什' ,'KRL'=>'库尔勒' ,'KCA'=>'库车' ,'KMG'=>'昆明' ,'KGT'=>'康定' ,
		'LHW'=>'兰州' ,'LXA'=>'拉萨' ,'LYG'=>'连云港' ,'LJG'=>'丽江' ,'LNJ'=>'临沧' ,'LYI'=>'临沂' ,'LZY'=>'林芝' ,'HZH'=>'黎平' ,'LZH'=>'柳州' ,'LYA'=>'洛阳' ,'JIU'=>'庐山' ,'LZO'=>'泸州' ,'LCX'=>'连城' ,'LLB'=>'荔波' ,'LCX'=>'龙岩' ,
		'LUM'=>'芒市' ,'NZH'=>'满洲里' ,'MXZ'=>'梅县' ,'MIG'=>'绵阳' ,'MDG'=>'牡丹江' ,'OHE'=>'漠河' ,
		'KHN'=>'南昌' ,'NAO'=>'南充' ,'NKG'=>'南京' ,'NNG'=>'南宁' ,'NTG'=>'南通' ,'NNY'=>'南阳' ,'NGB'=>'宁波' ,'NLT'=>'那拉提' ,
		'PZI'=>'攀枝花' ,
		'IQM'=>'且末' ,'TAO'=>'青岛' ,'IQN'=>'庆阳' ,'SHP'=>'秦皇岛' ,'NDG'=>'齐齐哈尔' ,'JJN'=>'泉州' ,'JUZ'=>'衢州' ,'JIQ'=>'黔江' ,
		'SYX'=>'三亚' ,'SHH'=>'上海' ,'SHA'=>'上海虹桥' ,'PVG'=>'上海浦东' ,'SWA'=>'汕头' ,'SHE'=>'沈阳' ,'SZX'=>'深圳' ,'SJW'=>'石家庄' ,'SYM'=>'思茅' ,'SHP'=>'山海关' ,
		'TCG'=>'塔城' ,'TVS'=>'唐山' ,'TYN'=>'太原' ,'TSN'=>'天津' ,'TGO'=>'通辽' ,'TEN'=>'铜仁' ,'TCZ'=>'腾冲' ,'THQ'=>'天水' ,'HYN'=>'台州' ,
		'HLH'=>'乌兰浩特' ,'URC'=>'乌鲁木齐' ,'WXN'=>'万州' ,'WEF'=>'潍坊' ,'WEH'=>'威海' ,'WNH'=>'文山' ,'WNZ'=>'温州' ,'WUA'=>'乌海' ,'WUH'=>'武汉' ,'WUX'=>'无锡' ,'WUS'=>'武夷山' ,'WUZ'=>'梧州' ,
		'XMN'=>'厦门' ,'XFN'=>'襄樊' ,'XIY'=>'西安' ,'XIC'=>'西昌' ,'XIL'=>'锡林浩特' ,'XNN'=>'西宁' ,'JHG'=>'西双版纳' ,'XUZ'=>'徐州' ,'ACX'=>'兴义' ,'DIG'=>'香格里拉' ,
		'ENY'=>'延安' ,'YNZ'=>'盐城' ,'YNJ'=>'延吉' ,'YNT'=>'烟台' ,'YBP'=>'宜宾' ,'YIH'=>'宜昌' ,'INC'=>'银川' ,'YIN'=>'伊宁' ,'YIW'=>'义乌' ,'LLF'=>'永州' ,'UYN'=>'榆林' ,'YCU'=>'运城' ,'LDS'=>'伊春' ,'YUS'=>'玉树' ,'YTY'=>'扬州' ,
		'DYG'=>'张家界' ,'ZHA'=>'湛江' ,'ZAT'=>'昭通' ,'CGO'=>'郑州' ,'HSN'=>'舟山' ,'ZUH'=>'珠海' ,'ZHY'=>'中卫' ,'HJJ'=>'芷江' ,'ZYI'=>'遵义' );
		return $codearr[$code];
	}
}
?>